package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/role")
@Controller
public class RoleController {

        @Reference
        private RoleService roleService;

    /**
     * 分页查询所有的角色
     * @param queryPageBean 分页条件
     * @return
     */
    @RequestMapping("/findAll")
    @ResponseBody
    public PageResult findAll(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult=null;
        try {
             pageResult=roleService.findAll(queryPageBean);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageResult;
    }

    @RequestMapping("/saveOne")
    @ResponseBody
    public Result saveOne(int[] ids, @RequestBody Role role){

        try {
            roleService.saveOne(ids,role);
            return new Result(true, "新增角色成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "新增角色失败!");
    }

    @RequestMapping("/selectBasicById/{id}")
    @ResponseBody
    public Result selectBasicById(@PathVariable int id){
        try {
            Role role= roleService.selectBasicById(id);
            return new Result(true, "查询角色基本信息成功!",role);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "查询角色基本信息失败!");
    }

    @RequestMapping("/selectMiddleByRoleId/{id}")
    @ResponseBody
    public Result selectMiddleByRoleId(@PathVariable int id){
        try {
            int[] ids=roleService.selectMiddleByRoleId(id);
            return new Result(true, "查询角色对应的权限成功!",ids);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new Result(false, "查询角色对应的权限失败!");
    }

    /**
     * 修改角色信息
     * @param newIds 角色关联的权限id数组
     * @param role 角色基本信息
     * @return
     */
    @RequestMapping("/updateOne")
    @ResponseBody
    public Result updateOne(int[] newIds,@RequestBody Role role){

        try {
            roleService.updateOne(newIds,role);
            return new Result(true, "修改角色信息成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "修改角色信息失败!");
    }

    @RequestMapping("/deleteRoleById/{id}")
    @ResponseBody
    public Result deleteRoleById(@PathVariable int id){
        try {
            roleService.deleteRoleById(id);
            return new Result(true, "删除角色成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, "删除角色失败!");
    }
}
